/**
 * Software License, Version 1.0
 * 
 * Copyright 2003 The Trustees of Indiana University.  All rights reserved.
 * 
 *
 *Redistribution and use in source and binary forms, with or without 
 *modification, are permitted provided that the following conditions are met:
 *
 *1) All redistributions of source code must retain the above copyright notice,
 * the list of authors in the original source code, this list of conditions and
 * the disclaimer listed in this license;
 *2) All redistributions in binary form must reproduce the above copyright 
 * notice, this list of conditions and the disclaimer listed in this license in
 * the documentation and/or other materials provided with the distribution;
 *3) Any documentation included with all redistributions must include the 
 * following acknowledgement:
 *
 *"This product includes software developed by the Community Grids Lab. For 
 * further information contact the Community Grids Lab at 
 * http://communitygrids.iu.edu/."
 *
 * Alternatively, this acknowledgement may appear in the software itself, and 
 * wherever such third-party acknowledgments normally appear.
 * 
 *4) The name Indiana University or Community Grids Lab or NaradaBrokering, 
 * shall not be used to endorse or promote products derived from this software 
 * without prior written permission from Indiana University.  For written 
 * permission, please contact the Advanced Research and Technology Institute 
 * ("ARTI") at 351 West 10th Street, Indianapolis, Indiana 46202.
 *5) Products derived from this software may not be called NaradaBrokering, 
 * nor may Indiana University or Community Grids Lab or NaradaBrokering appear
 * in their name, without prior written permission of ARTI.
 * 
 *
 * Indiana University provides no reassurances that the source code provided 
 * does not infringe the patent or any other intellectual property rights of 
 * any other entity.  Indiana University disclaims any liability to any 
 * recipient for claims brought by any other entity based on infringement of 
 * intellectual property rights or otherwise.  
 *
 *LICENSEE UNDERSTANDS THAT SOFTWARE IS PROVIDED "AS IS" FOR WHICH NO 
 *WARRANTIES AS TO CAPABILITIES OR ACCURACY ARE MADE. INDIANA UNIVERSITY GIVES
 *NO WARRANTIES AND MAKES NO REPRESENTATION THAT SOFTWARE IS FREE OF 
 *INFRINGEMENT OF THIRD PARTY PATENT, COPYRIGHT, OR OTHER PROPRIETARY RIGHTS. 
 *INDIANA UNIVERSITY MAKES NO WARRANTIES THAT SOFTWARE IS FREE FROM "BUGS", 
 *"VIRUSES", "TROJAN HORSES", "TRAP DOORS", "WORMS", OR OTHER HARMFUL CODE.  
 *LICENSEE ASSUMES THE ENTIRE RISK AS TO THE PERFORMANCE OF SOFTWARE AND/OR 
 *ASSOCIATED MATERIALS, AND TO THE PERFORMANCE AND VALIDITY OF INFORMATION 
 *GENERATED USING SOFTWARE.
 */
package cgl.narada.performance.gui;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.Vector;

import javax.swing.JFrame;

import cgl.narada.performance.LinkPerformanceDataImpl;
import cgl.narada.transport.LinkPerformanceData;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Company: </p>
 * @author Gurhan Gunduz
 * @version 1.0
 */

public class Chart extends JFrame {
  private long [] latency=new long[24];
  private boolean color=true;
  private int[] xValue={1,2,3,4,5,6,7,8,9,10,11,12};
  private int[] yValue={10,20,30,40,50,60,70};
  private int[] yValue1={1,2,3,4,5,6,7};

  private int xPos=47;
  private int []  xPosPlot={87,127,167,207,247,287,327,367,407,447,487,527, 567};
  private int [] latencies=new int[xPosPlot.length];
  private int [] std=new int[xPosPlot.length];
  private int [] jitter=new int[xPosPlot.length];

  private int counter=0, firstTime=0, value=0, endPoint=0, yCount=0;
  private double multiplier=1;
  private boolean largeScale=false, f=true;

//  private boolean notBeenThere=true;
//private LinkPerformanceData[] p_data;


  private Vector v=new Vector();

  private LinkPerformanceData[] p_data = {
      new LinkPerformanceDataImpl("Mean", "mean ", "ms"),
      new LinkPerformanceDataImpl("Std Dev", "Standart deviation", "ms"),
      new LinkPerformanceDataImpl("jitter", "Jitter", "ms"),
//    new LinkPerformanceDataImpl("Throughput", "Throughput", "KB/Sec"),
      new LinkPerformanceDataImpl("Loss Rate", "Loss Rate", "%"),

  };

  public Chart(String nameOfTheLink) {
    super("PERFORMANCE VALUES OF "+nameOfTheLink);
    //Graphics g;
    setSize(600,420);
    this.setBackground(Color.lightGray);
    this.setLocation(300,300);
    //this.show();


  }

  public void paint(Graphics g){

    Graphics2D g2=(Graphics2D) g;
    g2.clearRect(40, 40, 550, 390);
    g2.drawRect(500, 50,70, 70);
    g2.setColor(Color.gray);
    g2.fillRect(500, 50,70, 70);
    g2.setColor(Color.black);
    //g2.setBackground(Color.white);
    g2.drawLine(50, 50, 50,370);
    g2.drawLine(50,370,550,370);
    g2.drawLine(546,367, 550, 370);
    g2.drawLine(546,373, 550, 370);
    g2.drawString("Second",550,390);

    g2.drawLine(47,53, 50, 50);
    g2.drawLine(53,53, 50, 50);
    for (int i = 90; i < 550; i+=40) {
      g2.drawLine(i, 368, i, 372);
    }

    if (largeScale) {
      multiplier=3.5;
      for (int i = 90; i < 550; i += 40) {
        if (i < 370) {
          g2.drawLine(48, i, 52, i);
          g2.drawString(java.lang.Integer.toString(yValue[yValue.length -
              yCount - 1]), 30, i + 5);
          yCount++;
        }
      }
    }else{
      multiplier=35;
      g2.setColor(Color.lightGray);
      g2.fillRect(28,80,18,350);
      g2.setColor(Color.black);
      for (int i = 90; i < 550; i += 40) {
        if (i < 370) {
          g2.drawLine(48, i, 52, i);
          g2.drawString(java.lang.Integer.toString(yValue1[yValue1.length -
              yCount - 1]),30, i + 5);
          yCount++;
        }
      }

    }

    yCount=0;
    if (counter>=12){
      arraySlide(xValue);
      xValue[xValue.length - 1] = counter + 1;
    }
    for (int i = 0; i < 12; i++) {
      xPos=((xPos+40)%567);
      if (xPos==0) xPos=87;
      g2.drawString(java.lang.Integer.toString(xValue[i]), xPos, 385);
      //System.out.print(" "+xValue[i]);
    }

    g2.setColor(Color.red);
    g2.drawLine(510, 60, 530, 60);
    g2.drawString("Std", 540, 65);
    if (counter<13) endPoint=counter-1;
    else endPoint=11;
    for (int i = 0; i < endPoint; i++) {
      g2.drawLine(xPosPlot[i], 368-(int)(std[i]*multiplier)-getPos(std[i])*5,
                      xPosPlot[ i + 1],
                      368-(int)(std[i+1]*multiplier)-getPos(std[i+1])*5);
    }

    g2.setColor(Color.blue);
    g2.drawLine(510, 80, 530, 80);
    g2.drawString("Lat", 540, 85);

    for (int i = 0; i < endPoint ; i++) {
      g2.drawLine(xPosPlot[i], 366-(int)(latencies[i]*multiplier)-getPos(latencies[i])*5,
                      xPosPlot[ i + 1],
                      366-(int)(latencies[i+1]*multiplier)-getPos(latencies[i+1])*5);

    }
    g2.setColor(Color.green);
    g2.drawLine(510, 100, 530, 100);
    g2.drawString("Jit", 540, 105);

    for (int i = 0; i < endPoint; i++) {
      g2.drawLine(xPosPlot[i], 364-(int)(jitter[i]*multiplier)-getPos(jitter[i])*5,
                      xPosPlot[ i + 1],
                      364-(int)(jitter[i+1]*multiplier)-getPos(jitter[i+1])*5);

    }

  }










/*
  public void paint(Graphics g){

    Graphics2D g2=(Graphics2D) g;
    g2.clearRect(40, 40, 550, 390);
    g2.setBackground(Color.white);
    g2.drawLine(50, 50, 50,370);
    g2.drawLine(50,370,550,370);
    g2.drawLine(546,367, 550, 370);
    g2.drawLine(546,373, 550, 370);
    g2.drawString("Second",550,390);

    g2.drawLine(47,53, 50, 50);
    g2.drawLine(53,53, 50, 50);
    for (int i = 90; i < 550; i+=40) {
      g2.drawLine(i, 368, i, 372);
      if (i<370) {
        g2.drawLine(48,i,52,i);
        g2.drawString(java.lang.Integer.toString(yValue[yValue.length-yCount-1]),30, i+5);
        yCount++;
      }
    }
    yCount=0;
    if (counter>=12){
      arraySlide(xValue);
      xValue[xValue.length - 1] = counter + 1;
    }
    for (int i = 0; i < 12; i++) {
      xPos=((xPos+40)%567);
      if (xPos==0) xPos=87;
      g2.drawString(java.lang.Integer.toString(xValue[i]), xPos, 385);
      //System.out.print(" "+xValue[i]);
    }

    g2.setColor(Color.red);
    if (counter<13) endPoint=counter-1;
    else endPoint=11;
    for (int i = 0; i < endPoint; i++) {
      g2.drawLine(xPosPlot[i], 368-(int)(std[i]*3.5)-getPos(std[i])*5,
                      xPosPlot[ i + 1],
                      368-(int)(std[i+1]*3.5)-getPos(std[i+1])*5);
    }

    g2.setColor(Color.blue);
    for (int i = 0; i < endPoint ; i++) {
      g2.drawLine(xPosPlot[i], 366-(int)(latencies[i]*3.5)-getPos(latencies[i])*5,
                      xPosPlot[ i + 1],
                      366-(int)(latencies[i+1]*3.5)-getPos(latencies[i+1])*5);

    }
    g2.setColor(Color.green);
    for (int i = 0; i < endPoint; i++) {
      g2.drawLine(xPosPlot[i], 364-(int)(jitter[i]*3.5)-getPos(jitter[i])*5,
                      xPosPlot[ i + 1],
                      364-(int)(jitter[i+1]*3.5)-getPos(jitter[i+1])*5);

    }

  }
*/
  public int getPos(int num){

    if (num>70) return 6;
    if (num>60) return 5;
    if (num>50) return 4;
    if (num>40) return 3;
    if (num>30) return 2;
    if (num>20) return 1;
    if (num>10) return 0;
    return 0;
  }

  public int[] arraySlide(int [] array){
    for (int i = 0; i < array.length-1; i++) {
      array[i]=array[i+1];
    }
    return array;
  }

  public void setMonitoringData(LinkPerformanceData[] lpd){
    int l=0,s=0,j=0;
    if (firstTime>4){
      l=(int)Double.parseDouble(lpd[0].getParameterValue());
      s=(int)Double.parseDouble(lpd[1].getParameterValue());
      j=(int)Double.parseDouble(lpd[2].getParameterValue());
      if (l>80) l=80;
      if (s>80) s=80;
      if (j>80) j=80;

      if (counter>=12){
        arraySlide(latencies);
        arraySlide(std);
        arraySlide(jitter);
//        latencies[11]=l;
//        std[11]=s;
//        jitter[11]=j;
        latencies[11]=l;
        std[11]=s;
        jitter[11]=j;
      }else{
        latencies[counter%12]=l;
        std[counter%12]=s;
        jitter[counter%12]=j;

//       latencies[counter%24]=(int)Double.parseDouble(lpd[0].getParameterValue());
//       std[counter%24]=(int)Double.parseDouble(lpd[1].getParameterValue());
//       jitter[counter%24]=(int)Double.parseDouble(lpd[2].getParameterValue());
      }
      if (counter==0) counter++;
      else{
        if (isBig(latencies,std,jitter)) largeScale=true;
          else {largeScale=false;}

        this.repaint();
        counter++;
      }
    }
    firstTime++;
  }

  public boolean isBig(int[] l, int[] s, int[] j ){
    for (int i = 0; i < l.length; i++) {
      if (l[i]>8 || s[i]>8 || j[i]>8) return true;
    }
    return false;
  }

  public void copyMonitoringData(LinkPerformanceData[] p){
    for (int i = 0; i < p.length; i++) {
      p_data[i].setParameterValue(p[i].getParameterValue());

    }


  }


  public void setLatencies(long [] values){
    latency = new long[values.length];
    for (int i = 0; i < values.length; i++) {
      latency[i]=values[i];
    }
//    for (int i = 0; i < latency.length; i++) {
//      System.out.print(latency[i]);
//    }
    this.repaint();

  }

  public static void main(String args[]){
    Chart c=new Chart("a");

//    long[] lat = {
//        50, 30, 20, 120, 110, 100, 150, 122, 134};
//    c.setLatencies(lat);
    c.show();
//    c.repaint();
//    c.repaint();
//



//    long[] lat1 = {
//        150, 130, 120, 200, 110, 300, 200, 222, 234, 150,120};
//    c.setLatencies(lat1);
//    c.show();
//    long[] lat2 = {
//        150, 130, 120, 200, 110, 300, 200, 222, 234,150,120,170,200};
//    c.setLatencies(lat2);
//    c.show();
//    long[] lat3 = {
//        150, 130, 120, 200, 110, 300, 200, 222, 234};
//    c.setLatencies(lat3);
//    c.show();



  }



}
